from rest_framework import serializers
from orders.models import OrderInfo, OrderGoods
from goods.models import SKU


class SKUSimpleSerializer(serializers.ModelSerializer):
    """ SKU 简单 模型类序列化器 """

    class Meta:
        model = SKU
        fields = ["name", "default_image"]


class OrderGoodsSerializer(serializers.ModelSerializer):
    """ 订单商品 模型类序列化器 """
    # sku是当前订单商品OrderGoods对象关联的单一SKU主表对象
    sku = SKUSimpleSerializer()

    class Meta:
        model = OrderGoods
        fields = ["count", "price", "sku"]


class OrderInfoDetailSerializer(serializers.ModelSerializer):
    """订单信息 详细 模型类序列化器 """
    user = serializers.StringRelatedField(read_only=True)
    # skus记录和表达，当前订单关联的多个从表OrderGoods对象
    skus = OrderGoodsSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = ["order_id", "create_time", "user", "total_count", "total_amount", "freight", "pay_method", "status",
                  "skus"]


class OrderInfoSimpleSerializer(serializers.ModelSerializer):
    """ 订单信息 简单 模型类序列化器 """
    # DateTimeField类型，默认序列化结果为iso标准时间字符串格式：2021-04-27T10:56:55.861885+08:00
    # 通过其约束条件format来自定义日期输出字符串格式
    create_time = serializers.DateTimeField(format="%Y-%m-%d")

    class Meta:
        model = OrderInfo
        fields = ["order_id", "create_time"]
